//
//  KDPatientAppliListViewController.m
//  LaiKeDaoClient
//
//  Created by dongkedi的iMac on 15/11/25.
//  Copyright © 2015年 LKD. All rights reserved.
//
//加号申请列表
#import "KDPatientAppliListViewController.h"
#import "KDPatientAppliBackView.h"
#import "KDTitleView.h"
#import "KDPatientAppliTableViewCell.h"
@interface KDPatientAppliListViewController ()<KDTitleViewDelegate,UITableViewDataSource,UITableViewDelegate>
{
    KDPatientAppliBackView  *_backView;
    KDTitleView             *_titleView;
    UITableView             *_tableView;
    int     _type;//0,未处理,1,已处理
}
@property (nonatomic,strong)NSArray     *dataArr;
@property (nonatomic,strong)NSMutableArray  *data1Arr;//未处理
@property (nonatomic,strong)NSMutableArray  *data2Arr;//已处理
@end

@implementation KDPatientAppliListViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    _type = 0;
    
    [self.view setBackgroundColor:[UIColor colorWithHexString:@"0xF2F2F2"]];
    [self setNaviTitle:@"加号申请"];
    [self setNaviLeftButton];
    
    self.data1Arr = [NSMutableArray array];
    self.data2Arr = [NSMutableArray array];

    
    [self createBackView];
    [self createUI];
}

- (void)createBackView
{
    _backView = [[KDPatientAppliBackView alloc] init];
    [self.view addSubview:_backView];
}

- (void)createUI
{
    _titleView = [[KDTitleView alloc] init];
    _titleView.clickDelegate = self;
    [_titleView.button1 setTitle:@"未处理" forState:UIControlStateNormal];
    [self.view addSubview:_titleView];
    _titleView.hidden = YES;
    
    _tableView = [[UITableView alloc] initWithFrame:(CGRect){0,HEIGHT(_titleView),kScreenWidth,kScreenHeight-HEIGHT(_titleView)-64} style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate   = self;
    _tableView.backgroundColor = [UIColor colorWithHexString:@"0xF2F2F2"];
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.view addSubview:_tableView];
    _tableView.hidden = YES;
}

- (void)viewWillAppear:(BOOL)animated
{
    SharedAppDelegate.tabBarController.tabBarIsHiden = YES;
    [self requestPatientAppliList];
}

- (void)viewWillDisappear:(BOOL)animated
{
    SharedAppDelegate.tabBarController.tabBarIsHiden = NO;
}

//titleview delegate
- (void)buttonClick:(int)whichButton
{
    if (whichButton == 1) {
        _type = 0;
    }else if (whichButton == 2){
        _type = 1;
    }
    [_tableView reloadData];
}

//网络交互
//patientAppliList
- (void)requestPatientAppliList
{
    [CSHApiClient patientAppliListDidFinishLoaded:^(KXJson *json, NSString *path) {
        //
        if ([[json getStringForKey:@"code"] intValue] == 200) {

            _backView.hidden = YES;
            _titleView.hidden = NO;
            _tableView.hidden = NO;
            [_titleView.button1 setTitle:@"未处理" forState:UIControlStateNormal];
            [_titleView.button2 setTitle:@"已处理" forState:UIControlStateNormal];
            
            NSArray *arr1 = [NSArray arrayWithArray:[json getObjectForKey:@"data"]];
            [self refeshArrs:arr1];
            [_tableView reloadData];
        }
    } didFailLoaded:^(NSError *error, NSString *path) {
        //
    }];
}

- (void)refeshArrs:(NSArray *)dataArr
{
    [self.data1Arr removeAllObjects];
    [self.data2Arr removeAllObjects];
    for (NSDictionary *dataDic in dataArr) {
        if ([dataDic[@"is_pass"] intValue] == 0) {
            [self.data1Arr addObject:dataDic];
        }else if ([dataDic[@"is_pass"] intValue] == 1){
            [self.data2Arr addObject:dataDic];
        }else if ([dataDic[@"is_pass"] intValue] == 2){
            [self.data2Arr addObject:dataDic];
        }else{
            [self.data1Arr addObject:dataDic];
        }
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (_type == 0) {
        return self.data1Arr.count;
    }else{
        return self.data2Arr.count;
    }
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 55;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CustomCellIdentifier = @"CellIdentifier";
    KDPatientAppliTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier];
    if (cell ==nil) {
        cell = [[KDPatientAppliTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CustomCellIdentifier];
        
        UIView  *line = [[UIView alloc] initWithFrame:(CGRect){0,54,WIDTH(cell.contentView),1}];
        [line setBackgroundColor:[UIColor colorWithHexString:@"0xF2F2F2"]];
        [cell.contentView addSubview:line];
    }
    
    if (_type == 0) {
        [cell reloadData:[self.data1Arr objectAtIndex:indexPath.row]];
    }else{
        [cell reloadData:[self.data2Arr objectAtIndex:indexPath.row]];
    }
    
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
